﻿using System;
using System.Collections.Generic;
using System.Text;
using Clipping.Infrastructure.Interfaces;
using Clipping.Infrastructure.Models;
using System.Text.RegularExpressions;

namespace Clipping.SearchEnginePageParsers
{
	public class GoogleBlogPageParser : ISearchEnginePageParse
	{
		public void Parse(string html, out List<Infrastructure.Models.News> news, out string nextPageUrl)
		{
			news = new List<News>();
			nextPageUrl = null;
			HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
			doc.LoadHtml(html);

			var liNav = doc.CreateNavigator();

			var lis = liNav.Select("//div[@class='vsc']");
			while (lis.MoveNext())
			{
				News n = new News();
				var li = lis.Current;

				//取标题和链接
				var linkNode = li.SelectSingleNode("descendant::h3/a");
				if (linkNode != null)
				{
					n.Title = linkNode.Value;
					n.Url = linkNode.GetAttribute("href", "");
				}

				//取摘要
				var summaryNode = li.SelectSingleNode("descendant::div[@class='st']");
				if (summaryNode != null)
				{
					n.Summary = summaryNode.Value;
				}

				var mediaNode = li.SelectSingleNode("descendant::div[@class='kv']");
				if (mediaNode != null)
				{
					n.Media = mediaNode.Value;
				}

				//取时间
				var authorAndDateNode = li.SelectSingleNode("descendant::div[@class='f slp']");
				if (authorAndDateNode != null)
				{

					string authorAndDateStr = authorAndDateNode.Value;
					Match match = Regex.Match(authorAndDateStr, @"([\s\S]*)发表于([\s\S]*)");
					if (match.Success)
					{
						n.Author = match.Groups[1].Value;
						string dateStr = match.Groups[2].Value;

						if (Regex.IsMatch(dateStr, @"\d+年\d+月\d+日"))
						{
							n.PubDate = Regex.Match(dateStr, @"(\d+年\d+月\d+日)").Groups[1].Value;
						}
						else
						{
							var timeSpanMatch = Regex.Match(dateStr, @"(\d+)\s*(小时|分钟|天)前", RegexOptions.None);
							if (timeSpanMatch.Success)
							{
								string valueStr = timeSpanMatch.Groups[1].Value;
								int value = int.Parse(valueStr);
								if (timeSpanMatch.Groups[2].Value == "小时")
								{
									n.PubDate = DateTime.Now.AddHours(0 - value).ToShortDateString();
								}
								else if (timeSpanMatch.Groups[2].Value == "分钟")
								{
									n.PubDate = DateTime.Now.AddMinutes(0 - value).ToShortDateString();
								}
								else
								{
									n.PubDate = DateTime.Now.AddDays(0 - value).ToShortDateString();
								}
							}
						}
					}
				}
				news.Add(n);
			}

			var nextPageNode = liNav.SelectSingleNode("//a[@id='pnnext']");
			if (nextPageNode != null)
			{

				nextPageUrl = System.Web.HttpUtility.HtmlDecode(nextPageNode.GetAttribute("href", ""));
			}
			else
			{
				var footDiv = liNav.SelectSingleNode("//div[@id='foot']");
				if (footDiv != null)
				{
					var aList = footDiv.Select("descendant::a");
					if (aList != null)
					{
						while (aList.MoveNext())
						{
							var a = aList.Current;
							if (a.Value.Contains("下一页"))
							{
								nextPageUrl = System.Web.HttpUtility.HtmlDecode(a.GetAttribute("href", ""));
								break;
							}
						}
					}
				}
			}
		}
	}
}
